<?php

if (!defined('BASEPATH'))
	exit('No Direct Access Allowed !');



class pengguna extends CI_Controller
{
	
	function __construct()
	{

		parent::__construct();
		$this->load->model('m_pengguna');

	}




	function index(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$data['data'] = $this->m_pengguna->ambil();
			$this->load->view('global/header');
			$this->load->view('pengguna/index', $data);
			$this->load->view('global/footer');
		}
	}

	function tambah(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else {
			$d['tipe'] = 'Tambah';
			$d['nama'] = "";
			$d['username'] = "";
			$d['email'] = "";
			$d['nomor_hp'] = "";
			$d['password'] = "";
			$d['id'] = "";
			$this->load->view('global/header');
			$this->load->view('pengguna/tambah', $d);
			$this->load->view('global/footer');
		}
	}


	function hapus($id='') {
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$this->m_pengguna->hapus($id);
			redirect('pengguna');
		}
	}




	function edit($id=''){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('admin'));
		} else {
			$ambil = $this->m_pengguna->ambilbyid($id)->row();
			$d['nama'] = $ambil->nama;
			$d['username'] = $ambil->username;
			$d['email'] = $ambil->email;
			$d['nomor_hp'] = $ambil->nomor_hp;
			$d['password'] = $ambil->password;
			$d['tipe'] = 'Edit';
			$d['id'] = $id;
			$this->load->view('global/header');
			$this->load->view('pengguna/tambah', $d);
			$this->load->view('global/footer');

		}
	}


	function simpan_pengguna(){
		$cek = $this->session->userdata('logged_in');
		if (empty($cek)) {
			header('location:' . site_url('login'));
		} else { 
			$this->form_validation->set_rules('nama', 'Nama Pengguna', 'trim|required');
			$this->form_validation->set_rules('username', 'Username', 'trim|required');
			$this->form_validation->set_rules('email','Email','trim|required|valid_email');
			$this->form_validation->set_rules('no_hp', 'Nomor Handphone', 'trim|required|numeric');
			$this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[6]');
			if ($this->input->post('submit')) {
				if ($this->form_validation->run() == FALSE) {
					$data['nama'] = $this->input->post('nama');
					$data['username'] = $this->input->post('username');
					$data['email'] = $this->input->post('email');
					$data['nomor_hp'] = $this->input->post('no_hp');
					$data['password'] = $this->input->post('password');
					$data['id'] = $this->input->post('id');
					$data['tipe'] = $this->input->post('tipe');
					$this->load->view('global/header');
					$this->load->view('pengguna/tambah', $data);
					$this->load->view('global/footer');
				}else{
					if( $this->input->post('tipe')== 'Tambah'){
						$this->m_pengguna->tambah();
						redirect('pengguna/tambah');
					}else{
						$id = $this->input->post('id');
						$this->m_pengguna->update($id);
						redirect('pengguna/edit/'.$id);
					}
				}
			}
		}
	}

}